<%@page import="uc3.controller.*"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="archsw0904.controller.catalog.*"%>
<%
	request.setCharacterEncoding(application
			.getInitParameter("PARAMETER_ENCODING"));
%>
<jsp:useBean id="usercache" scope="session"
	class="session.cache.UserCache" />
<jsp:useBean id="booksCache" scope="request"
	class="session.cache.BookQueryCache" />
<%!private static final QueryableField[] FIELDS = { QueryableField.Isbn,
			QueryableField.Title, QueryableField.Author, QueryableField.Editor,
			QueryableField.Price };
	private static final SearchOrder[] SEARCH_ORDERS = { SearchOrder.Ascending,
			SearchOrder.Descending, SearchOrder.Default };
	private static final String ORDER_BY = "orderBy";
	private static final String ORDER_TYPE = "orderType";

	private QueryParam getQueryParams(HttpServletRequest request) {
		List<QueryParamFieldsEntry> queryFields = new ArrayList<QueryParamFieldsEntry>();
		String param, orderParam, orderTypeParam;
		SearchOrder searchOrder, orderedFieldOrder;
		QueryableField orderedField;

		orderParam = request.getParameter(ORDER_BY);
		orderTypeParam = request.getParameter(ORDER_TYPE);
		orderedField = null;
		orderedFieldOrder = SearchOrder.Default;

		if (orderParam != null && orderParam.compareTo("") != 0) {
			for (QueryableField q : FIELDS) {
				if (orderParam.equalsIgnoreCase(q.getValue())) {
					orderedField = q;
				}
			}

			if (orderTypeParam != null && orderTypeParam.compareTo("") != 0) {
				for (SearchOrder s : SEARCH_ORDERS) {
					if (orderTypeParam.equalsIgnoreCase(s.getValue())) {
						orderedFieldOrder = s;
					}
				}
			}
		}

		for (QueryableField q : FIELDS) {

			param = request.getParameter(q.getValue().toLowerCase());
			if ((param != null) && (param.compareTo("") != 0)
					&& (q.equals(orderedField))) {
				queryFields
						.add(new QueryParamFieldsEntry(
								new QueryableFieldOrdered(orderedFieldOrder, q),
								param));
			} else if (q.equals(orderedField)) {
				queryFields.add(new QueryParamFieldsEntry(
						new QueryableFieldOrdered(orderedFieldOrder, q), null));
			} else if (param != null && param.compareTo("") != 0) {
				queryFields.add(new QueryParamFieldsEntry(
						new QueryableFieldOrdered(SearchOrder.Default, q),
						param));
			}
		}

		if (queryFields.size() != 0) {

			QueryParamFieldsEntry fields[] = new QueryParamFieldsEntry[queryFields
					.size()];

			return new QueryParam(queryFields.toArray(fields));
		} else {
			return null;
		}
	}%>
<%
	Book libri[] = booksCache.getBooks();
%>
<table border="1" style="border-style: solid; border-color: black;"
	cellspacing="0" cellpadding="3">
	<tr>
		<th>ISBN</th>
		<th>Title</th>
		<th>Author</th>
		<th>Editor</th>
		<th>Year</th>
		<th>Price</th>

		<%
			if (!usercache.isAdmin()) {
		%>
		<th>Add to Cart</th>
		<%
			}
		%>
	</tr>
	<%
		for (Book b : libri) {
	%>
	<tr>
		<td><%=b.getIsbn()%></td>
		<td><%=b.getTitle()%></td>
		<td><%=b.getAuthor()%></td>
		<td><%=b.getEditor()%></td>
		<td><%=b.getYear()%></td>
		<td><%=b.getPrice()%> &euro;</td>

		<%
			if (!usercache.isAdmin()) {
		%>
		<td align="center" valign="middle">
		<form method="post"
			action="<%=(usercache.isAuthenticatede() && usercache
									.isClient()) ? "/BookShopWeb/page/cart.jsp"
							: "/BookShopWeb/page/login.jsp"%>"
			style="margin: 0px; padding: 0px;">
		<%
			if (!usercache.isAuthenticatede()) {
		%> <input type="hidden" name="redirect"
			value="/BookShopWeb/page/cart.jsp" /> <input type="hidden"
			name="refererOverride"
			value="<%=request.getRequestURL() + "?"
								+ request.getQueryString()%>" />
		<%
			}
		%> <input type="hidden" name="addBook" value="<%=b.getId()%>"></input>
		<input type="submit" value="Add to Cart"></input></form>
		</td>
		<%
			}
		%>
	</tr>
	<%
		}
	%>
</table>